//
//  ZiXunViewController.m
//  huaerjieqidai
//
//  Created by 黄军 on 2017/7/13.
//  Copyright © 2017年 $. All rights reserved.
//

#import "ZiXunViewController.h"

#define RGB(r,g,b) [UIColor colorWithRed:((float)(r))/255.0 green:((float)(g))/255.0 blue:((float)(b))/255.0 alpha:1.0]

@interface ZiXunViewController ()<UIWebViewDelegate>
@property(nonatomic,strong)UIWebView *webView;
@end

@implementation ZiXunViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationLeft];
    [self createWebView];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBarTintColor:RGB(24, 38, 49)];
    self.navigationController.navigationBar.titleTextAttributes=@{NSForegroundColorAttributeName:[UIColor whiteColor],
                          NSFontAttributeName:[UIFont boldSystemFontOfSize:17]};
}

-(void)initNavigationLeft{
    
    UIView *leftBtnView= [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 22 *2)];
    [leftBtnView setBackgroundColor:[UIColor clearColor]];
    
    UIButton *leftBtnBigger = [UIButton  buttonWithType:UIButtonTypeCustom];
    leftBtnBigger.backgroundColor = [UIColor clearColor];
    leftBtnBigger.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftBtnBigger setImage:[UIImage imageNamed:@"ico_backwhite"] forState:UIControlStateNormal];
    leftBtnBigger.titleLabel.font = [UIFont systemFontOfSize:14];
    [leftBtnBigger setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftBtnBigger addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [leftBtnView addSubview:leftBtnBigger];
    leftBtnBigger.frame = leftBtnView.frame;
//    [leftBtnBigger mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(leftBtnView);
//    }];

    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtnView];
    
    
    if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)){
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                           target:nil action:nil];
        negativeSpacer.width = -17;
        self.navigationItem.leftBarButtonItems = @[negativeSpacer, rightBarButtonItem];
    } else {
        self.navigationItem.leftBarButtonItem = rightBarButtonItem;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES ;
}

-(void)viewDidDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO ;
    [super viewDidDisappear:animated];
}

-(void)createWebView{
    self.webView  = [[UIWebView alloc]init];
    self.webView.delegate = self;
    self.webView.frame =CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    NSURL *url = [NSURL URLWithString:@"http://hej1.shqlkj.com/index/qihuo"];
    
    
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];
    
    [self.view addSubview:self.webView];
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//    self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"开始请求");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"请求完成");
    NSString *swizzleCloseWindowJS =
    @"var script = document.createElement('script');"
    "script.type = 'text/javascript';"
    "script.text = \"window.close = function windowOnClose() { "
    "javascript:window.location='hackCloseWindowFrame://'"
    "}\";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    [webView stringByEvaluatingJavaScriptFromString:swizzleCloseWindowJS];
    
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"请求失败：%@",error.userInfo);
}


-(void)back:(UIButton*)sender{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }else{
        self.tabBarController.selectedIndex = 0;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
